package com.dotc.patterndemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.dotc.patterndemo.action.observer.CustomerObserver;
import com.dotc.patterndemo.action.observer.MangoAttention;
import com.dotc.patterndemo.action.strategy.Discount;
import com.dotc.patterndemo.action.strategy.FullDiscount;
import com.dotc.patterndemo.action.strategy.NewerDiscount;
import com.dotc.patterndemo.action.strategy.SecondDiscount;
import com.dotc.patterndemo.action.template.ShoppingCart;
import com.dotc.patterndemo.create.builder.Builder;
import com.dotc.patterndemo.create.builder.FruitMeal;
import com.dotc.patterndemo.create.builder.HolidayBuilder;
import com.dotc.patterndemo.create.factory.abstractFacttory.AbstractFactory;
import com.dotc.patterndemo.create.factory.abstractFacttory.AppleFactory;
import com.dotc.patterndemo.create.factory.factoryMethod.AppleFactory2;
import com.dotc.patterndemo.create.factory.simple.StaticFactory;
import com.dotc.patterndemo.entity.Bag;
import com.dotc.patterndemo.entity.Fruit;
import com.dotc.patterndemo.entity.Fruitt.Orange;
import com.dotc.patterndemo.entity.bag.AppleBag;
import com.dotc.patterndemo.entity.bag.OrangeBag;
import com.dotc.patterndemo.structure.adapter.OrangeBagAdapter;
import com.dotc.patterndemo.structure.bridge.Bag.BagAbstraction;
import com.dotc.patterndemo.structure.bridge.Bag.BigBag;
import com.dotc.patterndemo.structure.bridge.material.Material;
import com.dotc.patterndemo.structure.bridge.material.Sack;
import com.dotc.patterndemo.structure.composite.DistrictNode;
import com.dotc.patterndemo.structure.composite.LeafNode;
import com.dotc.patterndemo.structure.decorator.CheckedBagDecorator;
import com.dotc.patterndemo.structure.decorator.ReinforceBagDecorator;
import com.dotc.patterndemo.structure.decorator.SpeedDecorator;
import com.dotc.patterndemo.structure.facade.PackService;
import com.dotc.patterndemo.structure.facade.PickService;
import com.dotc.patterndemo.structure.facade.SendService;
import com.dotc.patterndemo.structure.proxy.OrderService;
import com.dotc.patterndemo.structure.proxy.ProxyOrder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    Button btn_1;
    Button btn_2;
    Button btn_3;
    Button btn_4;
    Button btn_5;
    Button btn_6;
    Button btn_7;
    Button btn_8;
    Button btn_9;
    Button btn_10;
    Button btn_11;
    Button btn_12;
    Button btn_13;
    Button btn_14;
    Button btn_15;
    Button btn_16;

    String TAG = "LUO";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);
        btn_3 = findViewById(R.id.btn_3);
        btn_4 = findViewById(R.id.btn_4);
        btn_5 = findViewById(R.id.btn_5);
        btn_6 = findViewById(R.id.btn_6);
        btn_7 = findViewById(R.id.btn_7);
        btn_8 = findViewById(R.id.btn_8);
        btn_9 = findViewById(R.id.btn_9);
        btn_10 = findViewById(R.id.btn_10);
        btn_11 = findViewById(R.id.btn_11);
        btn_12 = findViewById(R.id.btn_12);
        btn_13 = findViewById(R.id.btn_13);
        btn_14 = findViewById(R.id.btn_14);
        btn_15 = findViewById(R.id.btn_15);
        btn_16 = findViewById(R.id.btn_16);
        initEvent();
        initEvent2();
    }

    private void initEvent() {

        //外观模式
        btn_11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initEvent11();
            }
        });

        //模板方法模式
        btn_12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initEvent12();
            }
        });


        //策略模式
        btn_14.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initEvent14();
            }
        });


        //责任链模式
        btn_15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initEvent15();
            }
        });

        //观察者模式
        btn_16.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initEvent16();
            }
        });
    }

    private void initEvent16() {
        MangoAttention attentions = new MangoAttention();//目标
        attentions.add(new CustomerObserver("deer"));
        attentions.add(new CustomerObserver("james"));
        attentions.add(new CustomerObserver("lison"));
        attentions.add(new CustomerObserver("mark"));

        //到货
        attentions.perform();
    }

    private void initEvent14() {


    }

    private void initEvent15() {
    }
    private void initEvent11() {
        PickService pickService = new PickService();
        PackService packService = new PackService();
        SendService sendService = new SendService();

        //采摘
        Log.d("LUO","--------------");
        pickService.doPick();

        //包装
        Log.d("LUO","--------------");
        packService.doPack();
        //快递
        Log.d("LUO","--------------");
        sendService.doSend();

    }

    /**
     * 模板方法模式
     */
//    private static Map<String, Discount> disCounts = new HashMap();
//    static {
//        disCounts.put("full",new FullDiscount());
//        disCounts.put("newer",new NewerDiscount());
//        disCounts.put("second",new SecondDiscount());
//    }
    private void initEvent12() {
       /* List<Fruit> products = new ArrayList();

        products.add(StaticFactory.getFruitApple());
        products.add(StaticFactory.getFruitBanana());
        products.add(StaticFactory.getFruitOrange());


        ShoppingCart cart = new OtherPayShopping(products);

        cart.submitOrder();*/

    }

    private void initEvent8() {
        AbstractFactory factory = new AppleFactory();
        //得到水果
        Fruit fruit = factory.getFruit();
        fruit.draw();
        //得到包装
        Bag bag = factory.getBag();

        //现需要增加防伪标识
        bag = new CheckedBagDecorator(bag);//防伪功能
        bag = new ReinforceBagDecorator(bag);//加固功能
        bag = new SpeedDecorator(bag);//加急功能
        bag.pack();
    }


    private void initEvent9() {
        //本地代理类
        OrderService orderService = new ProxyOrder();
        orderService.saveOrder();
    }
    private void initEvent10() {
        //根目录
        DistrictNode root = new DistrictNode("根");

        //一线目录
        root.addChild(new DistrictNode("上海"));
        root.addChild(new DistrictNode("天津"));
        DistrictNode districtNode = new DistrictNode("北京");
        root.addChild(districtNode);

        //二级目录
        districtNode.addChild(new DistrictNode("海淀区"));
        districtNode.addChild(new DistrictNode("西城区"));
        DistrictNode districtNode2 = new DistrictNode("朝阳区");
        districtNode.addChild(districtNode2);

        //三级目录
        districtNode2.addChild(new LeafNode("三里屯"));
        districtNode2.addChild(new LeafNode("朝阳外街"));

       // System.out.println(JSON.toJSON(root));
        Log.d("LUO",""+JSON.toJSON(root));

    }
    private void initEvent2() {

        //简单工厂模式
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //简单工厂模式
                Fruit fruit = StaticFactory.getFruit(StaticFactory.TYPE_APPLE);
                fruit.draw();
                //多工厂模式调用
//              Fruit fruitOrange = StaticFactory.getFruitOrange();
//              fruitOrange.draw();
            }
        });

        //工厂方法模式
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppleFactory2 fruitFactory = new AppleFactory2();
                Fruit fruit = fruitFactory.getFruit();
                fruit.draw();
            }
        });

        //抽象工厂模式
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //初始化工厂
                AbstractFactory factory = new com.dotc.patterndemo.create.factory.abstractFacttory.AppleFactory();
                //得到水果
                Fruit fruit = factory.getFruit();
                fruit.draw();
                //得到包装
                Bag bag = factory.getBag();
                bag.pack();
            }
        });

        //建造者模式
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //收银台
                Builder builder = new HolidayBuilder();
                //以下是模板代码，不轻易更改
                builder.buildApple(120);
                builder.buildBanana(80);
                builder.buildOrange(50);

                FruitMeal fruitMeal = builder.getFruitMeal();
                int cost = fruitMeal.cost();
                Log.d("LUO", "本次共花费：" + cost);
            }
        });

        //适配器模式
        btn_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //准备用途苹果盒代替
                Orange orange = new Orange("peter",100);
                OrangeBag bag = getBag2();
                orange.draw();
                orange.pack(bag);
            }
        });

        //桥接模式
        btn_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //袋子型号
                BagAbstraction bag = new BigBag();
                //袋子材质
                Material material = new Sack();
                bag.setMaterial(material);
                //开始采摘
                bag.pick();
            }
        });

        //装饰器模式
        btn_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initEvent8();
            }
        });

        //代理模式
        btn_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initEvent9();
            }
        });

        //组合模式
        btn_10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                initEvent10();
            }
        });
    }

    private static OrangeBag getBag2(){
        //准备用途苹果盒代替
        AppleBag appleBag = new AppleBag();
        //把苹果盒适配成桔子包装盒
        OrangeBag orangeBag = new OrangeBagAdapter(appleBag);
        return orangeBag;
    }

}